package com.demo.demo.jinhua;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2022/7/8
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 炸金花手牌信息类
 *
 * @author lzq31
 *
 */
public class FgfCardHand implements CardHand {


    public static Map HAND_TYPES = new HashMap();
    /**
     * 单散牌
     */
    public static int HAND_TYPE_VALUE_DSP = 1;
    /**
     * 对子牌
     */
    public static int HAND_TYPE_VALUE_DZP = 2;
    /**
     * 乱顺牌
     */
    public static int HAND_TYPE_VALUE_LSP = 3;
    /**
     * 乱金牌
     */
    public static int HAND_TYPE_VALUE_LJP = 4;
    /**
     * 顺金牌
     */
    public static int HAND_TYPE_VALUE_SJP = 5;
    /**
     * 豹子牌
     */
    public static int HAND_TYPE_VALUE_BZP = 6;


    static {
        HAND_TYPES.put(1, "单散牌");
        HAND_TYPES.put(2, "对子牌");
        HAND_TYPES.put(3, "乱顺牌");
        HAND_TYPES.put(4, "乱金牌");
        HAND_TYPES.put(5, "顺金牌");
        HAND_TYPES.put(6, "豹子牌");
    }


    private String playerName; // 玩家账号
    private Poker poker1;
    private Poker poker2;
    private Poker poker3;
    // 通过在构造器中调用getCardsTypeFun赋值，暴露getCardsType，删除setCardsType
    private int cardsType; // 牌型 比如豹子、金花等等


    // 构造器：将三种随机发的牌进行排序后赋给当前类的三个牌实例变量
    public FgfCardHand(String playerName, Poker poker1, Poker poker2, Poker poker3) {
        super();
        this.playerName = playerName;


        List<Poker> pokers = new ArrayList();
        pokers.add(poker1);
        pokers.add(poker2);
        pokers.add(poker3);
        Collections.sort(pokers);


        this.poker1 = pokers.get(0);
        this.poker2 = pokers.get(1);
        this.poker3 = pokers.get(2);


        this.cardsType = getCardsTypeFun();
    }


    /**
     * 获取当前对象牌型的方法
     *
     * @return
     */
    public int getCardsTypeFun() {
// 1:单张牌 2:对子牌3:乱顺牌4:乱金牌5:顺金牌6:豹子牌
// 判断是否是豹子牌型
        if (this.poker1.getPointValue() == this.poker2.getPointValue()
                && this.poker2.getPointValue() == this.poker3.getPointValue()) {


            return HAND_TYPE_VALUE_BZP;
        }
// 判断是否是金牌
        if (this.poker1.getTypeValue() == this.poker2.getTypeValue()
                && this.poker2.getTypeValue() == this.poker3.getTypeValue()) {
// 是否是顺金
            if (this.poker3.getPointValue() - this.poker2.getPointValue() == 1
                    && this.poker2.getPointValue() - this.poker1.getPointValue() == 1) {
                return HAND_TYPE_VALUE_SJP;
            }
// 乱金牌
            else {
                return HAND_TYPE_VALUE_LJP;
            }
        } else {
// 是否是乱顺牌
            if (this.poker3.getPointValue() - this.poker2.getPointValue() == 1
                    && this.poker2.getPointValue() - this.poker1.getPointValue() == 1) {
                return HAND_TYPE_VALUE_LSP;
            } else {
// 是否是对子牌
                if (this.poker1.getPointValue() == this.poker2.getPointValue()
                        || this.poker2.getPointValue() == this.poker3.getPointValue()
                        || this.poker1.getPointValue() == this.poker3.getPointValue()) {
                    return HAND_TYPE_VALUE_DZP;
                }
// 其他的都是单张牌
                else {
                    return HAND_TYPE_VALUE_DSP;
                }
            }
        }
    }


    @Override
    public String toString() {

        int score = Player.PLAYERS.get(this.playerName).getScore();

        return this.playerName + " " + HAND_TYPES.get(this.cardsType) + "[" + this.poker1 + ", " + this.poker2 + ", "
                + this.poker3 + "]\t当前积分:" + score ;
    }



//    @Override
//    public int compareTo(CardHand o) {
//        FgfCardHand cardHand = (FgfCardHand) o;
//        return FgfGameRuleService.getInstance().rule(cardHand, this);
//    }

    @Override
    public int compareTo(Object o) {
        FgfCardHand cardHand = (FgfCardHand) o;
        return FgfGameRuleService.getInstance().rule(cardHand, this);
    }

    public String getPlayerName() {
        return playerName;
    }


    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }


    public Poker getPoker1() {
        return poker1;
    }


    public void setPoker1(Poker poker1) {
        this.poker1 = poker1;
    }


    public Poker getPoker2() {
        return poker2;
    }


    public void setPoker2(Poker poker2) {
        this.poker2 = poker2;
    }


    public Poker getPoker3() {
        return poker3;
    }


    public void setPoker3(Poker poker3) {
        this.poker3 = poker3;
    }


    public int getCardsType() {
        return cardsType;
    }

    public static void main(String[] args) {

        List<FgfCardHand> list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            String playerName = "PLAYER" + i;

            int j = (int) (Math.random() * 4);
            int k = (int) (Math.random() * 13);
            String tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker1 = new Poker(tp);

            j = (int) (Math.random() * 4);
            k = (int) (Math.random() * 13);
            tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker2 = new Poker(tp);

            j = (int) (Math.random() * 4);
            k = (int) (Math.random() * 13);
            tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker3 = new Poker(tp);

            FgfCardHand cardHand = new FgfCardHand(playerName, poker1, poker2, poker3);
            list.add(cardHand);

        }

        Collections.sort(list);

        for (FgfCardHand ch : list) {
            System.out.println(ch);
        }
    }


}







